package GameUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import Game.Challange;
import Game.Watch;
import Server.Client;
/**
 * This class for ui health bar.
 * @author wat wattanagaroon
 * @version 2013//05/10
 */
public class Uihealthbar extends JFrame implements Observer {
	public JProgressBar bar1;
	public JProgressBar bar2;
	private JLabel labeloftime;
	public final JLabel player1name;
	public final JLabel player2name;
	private JPanel panelhealth;
	private JPanel paneltextfield;
	public final JTextField inputfield;
	public final JTextField outputfield;
	public Watch watch;
	private Challange challange;
	private String name1 = "";
	private String name2 = "";
	public 	PictureUI picui = new PictureUI();
	private Client client;
	/**
	 * This is the ui health bar.
	 * @param watch count the time.
	 * @param challange the rule of the game.
	 */
	public Uihealthbar(Watch watch, Challange challange) {
		super("TYPING FIGHT!!!!!");
		this.challange = challange;
		this.watch = watch;
		inputfield = new JTextField(challange.sendWord());
		inputfield.setEditable(false);
		inputfield.setForeground(new Color(255, 0, 0));
		inputfield.setHorizontalAlignment(SwingConstants.CENTER);
		inputfield.setBackground(new Color(153, 204, 204));

		outputfield = new JTextField();
		outputfield.requestFocus();
		player1name = new JLabel();
		player1name.setFont(new Font("Tahoma", Font.PLAIN, 18));
		player1name.setHorizontalAlignment(SwingConstants.CENTER);
		player1name.setForeground(SystemColor.activeCaption);
		player2name = new JLabel();
		player2name.setForeground(SystemColor.activeCaption);
		player2name.setFont(new Font("Tahoma", Font.PLAIN, 18));
		inticomponent();
		run();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		watch.addObserver(this);

	}

	private void inticomponent() {
		// on the top of the frame health, time and name.
		panelhealth = new JPanel();
		panelhealth.add(player1name);
		bar1 = new JProgressBar(0, 100);
		bar1.setForeground(new Color(0, 204, 51));
		bar1.setValue(100);
		bar1.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				JProgressBar bar = (JProgressBar) e.getSource();
				if(bar.getValue() < 70)
					bar.setForeground(Color.YELLOW);
				else if(bar.getValue() < 50)
					bar.setForeground(Color.ORANGE);
				else
					bar.setForeground(Color.RED);
				if (bar.getValue() == 0) {
					if (JOptionPane.showConfirmDialog(null,  name1+" is dead \n Do you want to play again?") == JOptionPane.YES_OPTION) {
						bar2.setValue(100);
						bar1.setValue(100);
						bar1.setForeground(new Color(0, 204, 51));
						bar2.setForeground(new Color(0, 204, 51));
						outputfield.setText(challange.sendWord());
					}
					else if (JOptionPane.showConfirmDialog(null, name1+" is dead \n Do you want to play again?") == JOptionPane.NO_OPTION) {
						System.exit(EXIT_ON_CLOSE);
					}

				}
			}
		});

		bar1.setStringPainted(true);
		panelhealth.add(bar1);
		labeloftime = new JLabel(watch.getTime() + "");
		labeloftime.setForeground(Color.RED);
		labeloftime.setFont(new Font("Tahoma", Font.PLAIN, 23));
		panelhealth.add(labeloftime);

		bar2 = new JProgressBar(0, 100);
		bar2.setForeground(new Color(0, 204, 51));
		bar2.setValue(100);
		bar2.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				JProgressBar bar = (JProgressBar) e.getSource();
				if(bar.getValue() < 70)
					bar.setForeground(Color.YELLOW);
				else if(bar.getValue() < 50)
					bar.setForeground(Color.ORANGE);
				else
					bar.setForeground(Color.RED);
				if (bar.getValue() == 0) {
					if (JOptionPane.showConfirmDialog(null, name2+" is dead \n Do you want to play again?") == JOptionPane.YES_OPTION) {
						bar2.setValue(100);
						bar1.setValue(100);
						bar1.setForeground(new Color(0, 204, 51));
						bar2.setForeground(new Color(0, 204, 51));
						outputfield.setText(challange.sendWord());
					}
					else if (JOptionPane.showConfirmDialog(null, name2+" is dead \n Do you want to play again?") == JOptionPane.NO_OPTION) {
						System.exit(EXIT_ON_CLOSE);
					}
				}
			}
		});
		bar2.setStringPainted(true);
		panelhealth.add(bar2);
		panelhealth.add(player2name);
		panelhealth.setLayout(new FlowLayout());
		super.add(panelhealth, BorderLayout.NORTH);
		// Create picture ui
		picui.run();
		// add pictureui to the frame
		add(picui.panelpicturecenter, BorderLayout.CENTER);

		// Declare textfield

		inputfield.setFont(new Font("System", Font.CENTER_BASELINE, 25));

		outputfield.setFont(new Font("System", Font.CENTER_BASELINE, 25));
		// add the textfield

		inputfield.setPreferredSize(new Dimension(50, 25));
		outputfield.setColumns(10);
		outputfield.setFocusable(true);
		outputfield.addKeyListener(new EnterAction());

		paneltextfield = new JPanel();
		paneltextfield.setLayout(new BoxLayout(paneltextfield, BoxLayout.X_AXIS));
		paneltextfield.add(inputfield);
		paneltextfield.add(outputfield);

		// addWindowListener(new EnterAction());
		getContentPane().add(paneltextfield, BorderLayout.SOUTH);
		setPreferredSize(new Dimension(800, 420));

	}
	/**
	 * The run method of the programe
	 */
	public void run() {
		pack();
		this.setVisible(true);

	}
	/**
	 * The method update after get the notify
	 */
	@Override
	public void update(Observable o, Object arg) {
		if (arg instanceof Watch) {
			Watch watch = (Watch) arg;
			labeloftime.setText(watch.getTime() + "");
			if (watch.getTime() == 0) {
				watch.stopTime();
				try {
					challange.sendTimetoServer(60);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}

	}
	/**
	 * The sub class of enter action.
	 *
	 */
	class EnterAction implements KeyListener {

		@Override
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				if (outputfield.getText()
						.equalsIgnoreCase(inputfield.getText())) {
					outputfield.setForeground(Color.green);
					try {
						challange.sendTimetoServer(watch.getElapseTime());
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					watch.stopTime();
				} else {
					outputfield.setForeground(Color.red);
				}
			}

		}
		/**
		 * The method that key realeased enter.
		 */
		@Override
		public void keyReleased(KeyEvent e) {
			outputfield.setForeground(Color.black);

		}

		@Override
		public void keyTyped(KeyEvent e) {

		}
	}
	/**
	 * To set the name of the player1
	 * @param name the player.
	 */
	public void setName1(String name){
		name1 = name;
	}
	/**
	 * To set the name of the player2
	 * @param name the player
	 */
	public void setName2(String name){
		name2 = name;
	}

}
